package domain;

import java.io.*;
import java.net.*;

public class SendFile implements Runnable
{
	Socket server;
	int port = 21;

	String m_path;
	String m_hostname;
	Socket m_socket;

	private SendFile(String path)
	{
		m_path = path;
	}

	private SendFile(Socket socket, String path, String hostname)
	{
		m_path = path;
		m_hostname = hostname;
		m_socket = socket;
		
	}

	@Override
	public void run()
	{
		try
		{
			System.out.println("Attempting to send file! ");
			server = new Socket(m_socket.getInetAddress(),21);// TODO:read address from file

			File fp = new File(m_path);

			DataOutputStream dos = new DataOutputStream(
					server.getOutputStream());
			FileInputStream fis = new FileInputStream(fp);

			byte[] buf = new byte[1024];
			int temp = '?';
			System.out.println(temp);
			int num = fis.read(buf);
			System.out.print(num);
			while (num != -1)
			{
				dos.write(buf, 0, num);
				dos.flush();
				num = fis.read(buf);
				System.out.print((char) num);
			}

			dos.close();
			fis.close();

		}
		catch (Exception e)
		{
			System.out.println(e.getMessage());
		}
	}
}
